﻿/*----------------------------------------------------------------
// Copyright (C) 2009 
// 版权所有。 
//
// 文件名：
// 文件功能描述：员工信息管理
//
//
// 创建标识：
// 程序作者：翟允锋
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using System.Threading;
using EQT.Model;
using Wuqi.Webdiyer;

public partial class Employee : System.Web.UI.Page
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private DaDept dalDept = new DaDept();
    private DaEmployee dalEmp = new DaEmployee();
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        if (!IsPostBack)
        {
            DataSet dsDept = dalDept.Query(string.Empty);
            Utility.BindDataSet2Combox(dsDept, DDLdept, "Dept_Name", "Dept_ID");
            DDLdept.Items.Insert(0, new ListItem("全部", ""));
            Utility.BindDataSet2Combox(dsDept, CreateDept_ID, "Dept_Name", "Dept_ID");
            Utility.BindDataSet2Combox(dsDept, EditDept_ID, "Dept_Name", "Dept_ID");
            CreateBirthDay.Text = DateTime.Now.ToString("yyyy-MM-dd");
            EditBirthDay.Text = DateTime.Now.ToString("yyyy-MM-dd");
            AspNetPager.PageSize = 15;

            this.BtnQuery_Click(null, null);

            this.SetPanelVisiable(SummaryPanel);

        }
    }

    protected void AspNetPager_PageChanged(object sender, EventArgs e)
    {
        this.BtnQuery_Click(null, null);
    }

    protected void EditSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。

        try
        {
            DateTime.Parse(EditBirthDay.Text.Trim());
        }
        catch
        {
            OperationResult.Text = "请输入正确的出生日期！"; divResult.Visible = true;
            return;
        }
        MoEmployee mod = new MoEmployee();
        try
        {
            mod.Emp_Id = EditEmp_ID.Text;
            mod.Birthday = DateTime.Parse(EditBirthDay.Text.Trim());
            mod.Dept_Id = EditDept_ID.SelectedValue;
            mod.Fullname = EditFullName.Text.Trim();
            mod.Home_Addr = EditHome_Addr.Text.Trim();
            mod.Idcard = EditIDCard.Text.Trim();
            mod.Phone = EditPhone.Text.Trim();
            mod.Rem = EditRem.Text.Trim();
            mod.Sex_Code = EditSex_Code.SelectedValue;

            int rtn = dalEmp.Update(mod);
            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "员工信息修改成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "员工信息修改失败！"; divResult.Visible = true;
            }
        }
        catch(Exception ex)
        {
            log.Error("员工信息修改失败", ex);
            OperationResult.Text = "员工信息修改失败！"; divResult.Visible = true;
        }
    }

    protected void EditBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void CreateSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        try
        {
            DateTime.Parse(CreateBirthDay.Text.Trim());
        }
        catch
        {
            OperationResult.Text = "请输入正确的出生日期！"; divResult.Visible = true;
            return;
        }

        MoEmployee mod = new MoEmployee();

        try
        {
            mod.Emp_Id=SequenceKeys.GenerateEmpID();
            mod.Birthday = DateTime.Parse(CreateBirthDay.Text.Trim());
            mod.Dept_Id = CreateDept_ID.SelectedValue;
            mod.Fullname = CreateFullName.Text.Trim();
            mod.Home_Addr = CreateHome_Addr.Text.Trim();
            mod.Idcard = CreateIDCard.Text.Trim();
            mod.Phone = CreatePhone.Text.Trim();
            mod.Rem = CreateRem.Text.Trim();
            mod.Sex_Code = CreateSex_Code.SelectedValue;

            int rtn=dalEmp.Add(mod);

            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "员工信息创建成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "员工信息创建失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("员工信息创建失败", ex);
            OperationResult.Text = "员工信息创建失败！"; divResult.Visible = true;
        }
    }

    protected void CreateBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void CreateGo_Click(object sender, EventArgs e)
    {
        this.CreateCancel();
        this.SetPanelVisiable(CreatePanel);
    }

    protected void DetailBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void EditGo_Click(object sender, EventArgs e)
    {
        string strKey = DetailEmp_ID.Text;
        this.DisplayEditInfo(strKey);
        this.SetPanelVisiable(EditPanel);
    }

    protected void DeleteGo_Click(object sender, EventArgs e)
    {
        try
        {
            string strKey = DetailEmp_ID.Text;
            int rtn = this.DeleteInfo(strKey);

            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "员工信息删除成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "员工信息删除失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("员工信息删除失败", ex);
            OperationResult.Text = "员工信息删除失败！"; divResult.Visible = true;
        }

    }

    protected void GvDelete_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = ((GridView)gvr.NamingContainer).DataKeys[gvr.RowIndex].Value.ToString();

        this.DeleteInfo(strKey);
        this.BtnQuery_Click(null, null);
    }

    protected void GvEdit_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = ((GridView)gvr.NamingContainer).DataKeys[gvr.RowIndex].Value.ToString();

        this.DisplayEditInfo(strKey);
        this.SetPanelVisiable(EditPanel);
    }

    protected void GvDetail_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = ((GridView)gvr.NamingContainer).DataKeys[gvr.RowIndex].Value.ToString();

        this.DisplayDetailInfo(strKey);
        this.SetPanelVisiable(DetailPanel);
    }

    protected void SetPanelVisiable(Panel panel)
    {
        EditPanel.Visible = false;
        DetailPanel.Visible = false;
        CreatePanel.Visible = false;
        SummaryPanel.Visible = false;

        panel.Visible = true;
    }

    protected void BtnQuery_Click(object sender, EventArgs e)
    {
        //这里查询员工信息并绑定到gridview上。
        int pagecount = 0;
        string strCondition = "where 1=1 ";

        if (sender != null && e != null)
        {
            AspNetPager.CurrentPageIndex = 0;
        }

        if (!string.IsNullOrEmpty(DDLdept.SelectedValue))
        {
            strCondition = strCondition + "and  Dept_ID='" + DDLdept.SelectedValue + "'";
        }
        if (!string.IsNullOrEmpty(TxtEmpID.Text.Trim()))
        {
            strCondition = strCondition + "and  Emp_ID='" + TxtEmpID.Text.Trim() + "'";
        }
        this.EmpGridView.DataSource = dalEmp.GetEmploreeRecords(strCondition, AspNetPager.PageSize, AspNetPager.CurrentPageIndex - 1, out pagecount);

        EmpGridView.DataBind();

        AspNetPager.RecordCount = pagecount;

        this.SetPanelVisiable(SummaryPanel);
    }

    protected int DeleteInfo(string strKey)
    {
        //执行删除操作。
        return dalEmp.Delete(strKey);
    }

    protected void DisplayDetailInfo(string strKey)
    {
        //这里查询员工信息
        MoEmployee mod = dalEmp.GetEntity(strKey);

        //然后给查看视图赋值。
        DetailDept_ID.Text = dalDept.GetEntity(mod.Dept_Id).Dept_Name; ;
        DetailEmp_ID.Text = mod.Emp_Id.ToString();
        DetailFullName.Text = mod.Fullname;
        DetailHome_Addr.Text = mod.Home_Addr;
        DetailBirthDay.Text = mod.Birthday.ToString("yyyy-MM-dd");
        DetailPhone.Text = mod.Phone;
        DetailIDCard.Text = mod.Idcard;
        DetailRem.Text = mod.Rem;
        DetailSex_Code.Text = mod.Sex_Code == "0" ? "男" : "女";
    }

    protected void DisplayEditInfo(string strKey)
    {
        //这里查询员工信息
        MoEmployee mod = dalEmp.GetEntity(strKey);

        //然后给编辑视图赋值。
        EditDept_ID.SelectedValue = mod.Dept_Id;
        EditEmp_ID.Text = mod.Emp_Id;
        EditFullName.Text = mod.Fullname;
        EditHome_Addr.Text = mod.Home_Addr;
        EditBirthDay.Text = mod.Birthday.ToString("yyyy-MM-dd");
        EditPhone.Text = mod.Phone;
        EditIDCard.Text = mod.Idcard;
        EditRem.Text = mod.Rem;
        EditSex_Code.SelectedValue = mod.Sex_Code;
    }

    protected void CreateCancel()
    {
        CreateFullName.Text = "";
        CreateHome_Addr.Text = "";
        CreateIDCard.Text = "";
        CreatePhone.Text = "";
        CreateRem.Text = "";
        CreateSex_Code.SelectedValue = "0";
        CreateBirthDay.Text = "";
    }

    protected void BtnExport_Click(object sender, EventArgs e)
    {
        string strCondition = "where 1=1 ";

        if (!string.IsNullOrEmpty(DDLdept.SelectedValue))
        {
            strCondition = strCondition + "and  Dept_ID='" + DDLdept.SelectedValue + "'";
        }
        if (!string.IsNullOrEmpty(TxtEmpID.Text.Trim()))
        {
            strCondition = strCondition + "and  Emp_ID='" + TxtEmpID.Text.Trim() + "'";
        }
        DataSet ds = dalEmp.GetEmploreeRecords(strCondition);
        GridViewExport.DataSource = ds;
        GridViewExport.DataBind();

        GridViewExportUtil.Export("Emploree.xls", GridViewExport);
        GridViewExport.DataSource = null;
        GridViewExport.DataBind();
    }
}
